package com.ahu.qifei.common.bean;

import com.ahu.qifei.common.common.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author liumch
 * date 2019/12/26 15:35
 * description
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResultModel<T> {

    /**
     * 返回码
     */
    private int code;
    /**
     * 数据
     */
    private T data;
    /**
     * 错误信息
     */
    private String msg;

    public static <T> ResultModel<T> success(int code, T data, String msg) {
        ResultModel<T> resultModel = new ResultModel<>();
        resultModel.setCode(code);
        resultModel.setMsg(msg);
        resultModel.setData(data);
        return resultModel;
    }

    public static <T> ResultModel<T> success(T data) {
        ResultModel<T> resultModel = new ResultModel<>();
        resultModel.setCode(ResultEnum.SUCCESS.getCode());
        resultModel.setMsg(ResultEnum.SUCCESS.getMessage());
        resultModel.setData(data);
        return resultModel;
    }


    public static <T> ResultModel<T> success() {
        ResultModel<T> resultModel = new ResultModel<>();
        resultModel.setCode(ResultEnum.SUCCESS.getCode());
        resultModel.setMsg(ResultEnum.SUCCESS.getMessage());
        resultModel.setData(null);
        return resultModel;
    }

    public static <T> ResultModel<T> fail(Integer code, String msg) {
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(code);
        resultModel.setMsg(msg);
        return resultModel;
    }

    public static <T> ResultModel<T> fail(String msg) {
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(ResultEnum.SERVER_ERROR.getCode());
        resultModel.setMsg(msg);
        return resultModel;
    }

    public static <T> ResultModel<T> fail() {
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(ResultEnum.SERVER_ERROR.getCode());
        resultModel.setMsg(ResultEnum.SERVER_ERROR.getMessage());
        return resultModel;
    }
}
